﻿using AssetManegement.Filter;
using Common;
using IService.BaseService;
using Microsoft.AspNetCore.Mvc;
using Model;
using ModelDto.Request;
using ModelDto;
using Newtonsoft.Json;
using ModelDto.Response;
using AutoMapper;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Query;
using Repository;
using Microsoft.AspNetCore.SignalR;
using System.Security.Claims;

namespace AssetManegement.Controllers
{
    /// <summary>
    /// 打印模板
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ServiceFilter(typeof(MyTokenFilterAttribute))]
    //[ServiceFilter(typeof(MyApiResultFilterAttribute))]
    public class PrintTempController : ControllerBase
    {
        private readonly IPrintTempService printTempService;
        private readonly IMapper mapper;
        private readonly IHubContext<MyHub> _hubContext;
        public PrintTempController(IPrintTempService printTempService, IMapper mapper, IHubContext<MyHub> _hubContext)
        {
            this.printTempService = printTempService;
            this.mapper = mapper;
            this._hubContext = _hubContext;
        }
        /// <summary>
        /// 根据ID获取对应打印模板
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResultL> GetPrintTempById(DeleteRequest request)
        {
            //HttpContext.User.FindFirst(ClaimTypes.Name);
            JsonResultL resultL = new JsonResultL();
            var data = await printTempService.FindAsync(request.Id);
            if (data == null)
            {
                resultL.Fail("未找到打印模板,请联系管理员");
                return resultL;
            }

            return resultL.Sucess(EntityMapperHelper.Mapper<PrintTempRequest, PrintTemp>(data));

        }
        /// <summary>
        /// 获取表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResultL> GetPrintTempList()
        {
            JsonResultL resultL = new JsonResultL();
            var data = await printTempService.GetListCacheAsync();
            var Response = mapper.Map<List<PrintTempResponse>>(data);
            return resultL.Sucess(new { list = Response, total = Response.Count() });
        }
        /// <summary>
        /// 根据id删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> DeletePrintTemp(DeleteRequest request)
        {
            return await printTempService.DeleteByWhereAsync(f => f.Id == request.Id);
        }
        /// <summary>
        /// 保存（新增或修改）
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResultL> SavePrintTemp(PrintTempRequest request)
        {
            //await _hubContext.Clients.All.SendAsync("ReceiveMessage", "曾沁海");
            JsonResultL response = new JsonResultL();
            string tempUser = HttpContext.Request.Headers["LUserInfo"].ToString();
            AccountLoginInfo CurrentUser = JsonConvert.DeserializeObject<AccountLoginInfo>(tempUser);

            if (CurrentUser == null || CurrentUser.Account == null)
            {
                return response.TokenFail("用户异常");
            }
            if (string.IsNullOrEmpty(request.Id))
            {
                PrintTemp entity = new PrintTemp();
                entity.Height = request.Height;
                entity.PageHeight = request.PageHeight;
                entity.PageWidth = request.PageWidth;
                entity.Width = request.Width;
                entity.Title = request.Title;
                entity.Type = request.Type;
                entity.TempItems = request.TempItems;
                entity.CreateUser = CurrentUser.Account.AccountNo;
                var flag = await printTempService.AddAsync(entity);
                return flag ? response.Sucess("新增成功") : response.Fail("新增失败");
            }
            else
            {
                var entity = await printTempService.FindAsync(request.Id);
                entity.Height = request.Height;
                entity.PageHeight = request.PageHeight;
                entity.PageWidth = request.PageWidth;
                entity.Width = request.Width;
                entity.Title = request.Title;
                entity.Type = request.Type;
                entity.TempItems = request.TempItems;
                entity.UpdateTime = DateTime.Now;
                entity.UpdateUser = CurrentUser.Account.AccountNo;
                var flag = await printTempService.UpdateAsync(entity);
                return flag ? response.Sucess("修改成功") : response.Fail("修改失败");
            }
        }
    }
}
